package com.twu.biblioteca.commands;

import com.twu.biblioteca.Biblioteca;
import com.twu.biblioteca.Book;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Formatter;

public class BookListCommand extends BibliotecaCommand {
    public static final String LIST_MESSAGE = "We have the following books in stock: ";

    public BookListCommand(InputStream input, PrintStream output, Biblioteca biblioteca) {
        super("List Books", input, output, biblioteca);
    }

    public boolean run() {
        output.println(LIST_MESSAGE);
        output.println(getFormattedBookList());
        return false;
    }

    private String getFormattedBookList() {
        StringBuilder builder = new StringBuilder();
        Formatter formatter = new Formatter(builder);

        builder.append("--------------------------\n");
        formatter.format("%-24s%-16s%-4s\n", "Title", "Author", "Year");
        builder.append("--------------------------\n");

        for (Book book : biblioteca.getBooks()) {
            formatter.format("%-24s%-16s%-4d\n", book.getTitle(), book.getAuthor(), book.getYearPublished());
        }

        builder.append("--------------------------\n");

        return builder.toString();
    }
}
